package com.example.administrator.commonlibrary.utils.arouter;

import android.app.Activity;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.administrator.commonlibrary.R;

import java.io.Serializable;

/**
 * @author wang_hong_wei
 * @description 路由的跳转帮助类
 * @Date 2018-5-11
 */

public class ARouterUtils {

    private ARouterUtils() {
    }

    public static void startActivityAnim(Activity context, String activityUrl) {
        //这部分代码是切换Activity时的动画，看起来就不会很生硬
        ARouter.getInstance()
                .build(activityUrl)
                .withTransition(R.anim.slide_in_right, R.anim.slide_in_left)
                .navigation(context);
    }

    public static void startActivity(Activity context, String activityUrl) {
        //这部分代码是切换Activity时的动画，看起来就不会很生硬
        ARouter.getInstance()
                .build(activityUrl)
                .navigation(context);
    }

    //发送消息的方法
    public static <T> T notifyDataChange(Class<? extends T>  helloService) {
        return ARouter.getInstance().navigation(helloService);
    }



    public static IntentBuilder newIntent(String activityUrl) {
        return new IntentBuilder(activityUrl);
    }
    private static class IntentBuilder {
        private Postcard build;
        private IntentBuilder(String activityUrl) {
            build = ARouter.getInstance().build(activityUrl);
        }

        public IntentBuilder putExtra(String key,String value){
            build.withString(key,value);
            return this;
        }

        public IntentBuilder putExtra(String key,int value){
            build.withInt(key,value);
            return this;
        }

        public IntentBuilder putExtra(String key, long value) {
            build.withLong(key, value);
            return this;
        }

        public IntentBuilder putExtra(String key, float value) {
            build.withFloat(key, value);
            return this;
        }

        public IntentBuilder putExtra(String key, double value) {
            build.withDouble(key, value);
            return this;
        }
        public IntentBuilder putExtra(String key, Serializable value) {
            build.withSerializable(key, value);
            return this;
        }
        public IntentBuilder putExtra(String key, Object value) {
            build.withObject(key, value);
            return this;
        }

        public void startActivity(Activity context) {
            build.navigation(context);
        }

        public void startActivityForResult(Activity context, int requestCode) {
            build.navigation(context, requestCode);
        }

        public void startActivityAnim(Activity context) {
            //这部分代码是切换Activity时的动画，看起来就不会很生硬
            build
                    .withTransition(R.anim.slide_in_right, R.anim.slide_in_left)
                    .navigation(context);
        }
    }
}
